#include <iostream>
#include "memPage_t.h"
#include "memPool_t.h"
using namespace std;

int main() {
	int value=0;
	int ret=0;
	int pos=0;

	cout << "Please enter how many empty pages would you like in memPool: ";
	cin >> value;
	memPool_t mp(value);

	while(1) {
		cout <<
			"\nwrite(w) | read(r) | setCurPos(p) | createNewPages(n) | rcvPagesParams(f) | setPageSize(s)"
			<< "| getPageSize(S) | curPos(P) | isEmpy(e) | actualSize(a) | Capacity(C) | numPages(N) : ";

		char c;
		cin >> c;

		if(c!='P' && c!='p' && c!='w' && c!='r' && c!='s'
			&& c!='S' && c!='n' && c!='N' && c!='f' && c!='a'
			&& c!='e' && c!='C' && c!='q') {
				cout << "No such command. Try again (q to exit)." << endl;
				continue;
		}

		if (c == 'q') {
			break;
		}

		switch (c) {
		   case 'e' : //isEmpt
			   cout << "memPool is " << (mp.isEmpty() ? "empty" : "not empty") << endl;
			   break;
		   case 'a' : //getActualSize
			   cout << "Actual size of memPool is: " << mp.getActualSize() << endl;
			   break;
		   case 'C' : //getCapacity
			   cout << "Capacity of memPool is: " << mp.getCapacity() << endl;
			   break;
		   case 'N' : //getNumOfPages
			   cout << "Num of pages in memPool is: " << mp.getNumOfPages() << endl;			   
			   break;
		   case 'n' :  //setnumOfPages
			   cout << "Please enter how many pages would you like to create: ";
			   cin >> value;
			 
			   mp.createNewPages(value);
			   cout << "Created "<< value << " new pages" << endl;
			   break;
		   case 's' :  //setDefaultPageSize
			   cout << "Please enter the new page size: ";
			   cin >> value;

			   mp.setDefaultPageSize(value);
			   cout << "Default page size was set to "<< value << endl;
			   break;
		   case 'S' :  //getDefaultPageSize
			   cout << "Default page size is: " << mp.getDefaultPageSize() << endl;
			   break;
		   case 'P' :  //getCurrentPos
			   cout << "Current Position is: " << mp.getCurrentPos() << endl;
			   break;
		   case 'p' :  //setCurrentPos
			   cout << "Please enter the new position: ";
			   cin >> value;

			   ret = mp.setCurrentPos(value);
			   if (ret)
				   cout << "Current position was set to "<< value << endl;
			   else
				   cout << "Position is invalid, didn't change position"<< endl;
			   break;
		   case 'w' : //write
			   /*We write only int in this test although all types are supported*/ 
			   cout << "Please enter a number to write into memPool: " ;
			   cin >> value;
			   cout << "Please enter a position you would like to write to(-1 for current position): " ;
			   cin >> pos;
			   ret = pos == -1 ? mp.write(value, sizeof(int)) : mp.write(value, sizeof(int), pos);
			   
			   cout << "Write " << (ret ? "succeeded" : "didn't succeed") << endl;
			   break;
		   case 'r' : //read
			   cout << "Please enter a position you would like to read from(-1 for current position): " ;
			   cin >> pos;
			   
			   ret = pos == -1 ? mp.read(value, sizeof(int)) : mp.read(value, sizeof(int), pos);
			   if (ret)
				   cout << "Read succeeded, the value is: " << value << endl;
			   else
				   cout << "Read didn't succeed" << endl;
			   break;
		   case 'f' : //getFirstPage, getCurrentPage, getLastPage and memPage functions
			   memPage_t* page = mp.getFirstPage();
			   
			   cout << "First Page:" << endl;
			   cout << "ActualSize: " << page->getActualSize() << endl;
			   cout << "Capacity: " << page->getCapacity() << endl;
			   cout << "isEmpty: " << page->isEmpty() << endl;
			   cout << "isFull: " << page->isFull() << endl;
			   cout << "CurrentPos: " << page->getCurrentPos() << endl;

			   page = mp.getCurrentPage();
			   cout << "\nCurrent Page:" << endl;
			   cout << "ActualSize: " << page->getActualSize() << endl;
			   cout << "Capacity: " << page->getCapacity() << endl;
			   cout << "isEmpty: " << page->isEmpty() << endl;
			   cout << "isFull: " << page->isFull() << endl;
			   cout << "CurrentPos: " << page->getCurrentPos() << endl;

			   page = mp.getLastPage();
			   cout << "\nLast Page:" << endl;
			   cout << "ActualSize: " << page->getActualSize() << endl;
			   cout << "Capacity: " << page->getCapacity() << endl;
			   cout << "isEmpty: " << page->isEmpty() << endl;
			   cout << "isFull: " << page->isFull() << endl;
			   cout << "CurrentPos: " << page->getCurrentPos() << endl;

			   break;
		}
	}

	return 0;
}
